package celiasoft.itasalibtest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import celiasoft.itasalib.query.result.QueryResult;
import celiasoft.itasalib.query.result.QueryResultError;
import celiasoft.itasalib.structures.DataElement;
import celiasoft.itasalib.structures.MyItasaShow;


public class ItasaLibTestActivity extends Activity {

	@SuppressWarnings("unused")
	private static final String TAG = "itasalib.test.ItasaLibTestActivity";
	private final static String TEMP_APIKEY = "f5630249d30f459738da06afc9426e29";
	
	private AsyncQueryLoader _loader = new AsyncQueryLoader();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		/*
		Window window = getWindow();  
		window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);*/
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        System.setProperty("http.agent", "Italiansubs-android-lib-testapp");
        
        QueryFactory factory = QueryFactory.getInstance(this, ItasaLibTestActivity.TEMP_APIKEY);    
        factory.initializeAuthcode("9bd4dc9406f24a18bf16baf598436575");
        
        final Button load = (Button) findViewById(R.id.button1);
        /*
        final MyItasaLastSubsQuery query = factory.makeMyItasaLastSubsQuery();
        
        final IQueryTaskListener listener = new IQueryTaskListener(){

			public void onComplete(QueryResult result) {
				String text = "";
		        
		        if(result.isStatusOk()){
		        	List<DataElement> news = result.getDataResult();
		        	for(DataElement el: news){
		        		MyItasaSubtitle s = (MyItasaSubtitle)el;
		        		text +=  s.getId() + ") "+ s.getName() + "\n version:" + s.getVersion() + "\n download date:" + s.getDownloadDate()+ "\n";
		        	}
		        	text +=  "\n More data? " + result.isThereMoreData();
		        }else{
		        	QueryResultError err = result.getQueryResultError();
		        	text = "ERROR CODE:" + err.getErrorCode();
		        	text += "\nERROR MSG:" + err.getErrorMessage();
		        }
		        
		        TextView tv = (TextView) findViewById(R.id.debugText);
		        tv.setMovementMethod(new ScrollingMovementMethod());
		        tv.setText(text.isEmpty()?"Unable to load data.":text);
		        
		        load.setEnabled(true);
			}

			public void onStartTask() {
				load.setEnabled(false);
				
			}};
        
        */
        
        final ShowThumbQuery query = factory.makeThumbShowQuery(2897);        
        
        _loader.load(new Runnable(){
        	
        	
        	
        });

			public void onComplete(QueryResult result) {
				String text = "";
		        
		        if(result.isStatusOk()){
		        	List<DataElement> news = result.getDataResult();
		        	for(DataElement el: news){
		        		MyItasaShow s = (MyItasaShow)el;
		        		text +=  s.getName() + "\n subs:" + s.getVersionOfAvailableSubs() + "\n";
		        	}
		        	text +=  "\n More data? " + result.isThereMoreData();
		        }else{
		        	QueryResultError err = result.getQueryResultError();
		        	text = "ERROR CODE:" + err.getErrorCode();
		        	text += "\nERROR MSG:" + err.getErrorMessage();
		        }
		        
		        TextView tv = (TextView) findViewById(R.id.debugText);
		        tv.setMovementMethod(new ScrollingMovementMethod());
		        tv.setText(text.isEmpty()?"Unable to load data.":text);
		        
		        load.setEnabled(true);
			}

			public void onStartTask() {
				load.setEnabled(false);
				
			}};
        
        
        
        /**************************************/
        load.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				//new QueryTask(listener).execute(query);
			/*	
				View v = load.getRootView();
				v.setDrawingCacheEnabled(true);
				Bitmap b = v.getDrawingCache();
				*/
				String filename = Environment.getExternalStorageDirectory().toString() + "/screen-test.png";
				
				Process process = null;
				try {
					process = Runtime.getRuntime().exec("su -c cat /dev/graphics/fb0");
					process.waitFor();
				} catch (IOException e1) {
					e1.printStackTrace();
					return;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return;
				}
				
				Log.d(TAG, "here");
				InputStream is = process.getInputStream();
				Bitmap b = BitmapFactory.decodeStream(is);
				
				try {
				       FileOutputStream out = new FileOutputStream(filename);
				       b.compress(Bitmap.CompressFormat.PNG, 90, out);
				       out.flush();
		               out.close();
				} catch (Exception e) {
				       e.printStackTrace();
				}
				
				Log.d(TAG, "Filename:" + filename );
				
			}
        	
        });
        
        
        
        
        
	}
}